//
//  ECMaterailManageHomeViewController.m
//  ECERP.CRM
//
//  Created by huanggulong on 2018/11/21.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMaterailManageHomeViewController.h"
#import "GLScrollSegmentedBar.h"
#import "ECMaterailManageAddViewController.h"
#import "ECMaterailManageListViewController.h"

@interface ECMaterailManageHomeViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIView * searchToolView;

@property (nonatomic,strong) UISearchBar * searchBar;

@property (nonatomic,strong) UIScrollView * contentView;

@property(nonatomic , strong)GLScrollAutoSegmentedBar *  segmentedBar;

@end

@implementation ECMaterailManageHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商品管理";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(doAdd:)];
    
    [self setUI];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}

#pragma mark - event
- (IBAction)doAdd:(id)sender{
    ECMaterailManageAddViewController *vc = [[ECMaterailManageAddViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - private
- (void)setUI{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 80)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.mas_equalTo(80);
    }];
    
    //搜索视图
    self.searchBar.layer.cornerRadius = 16;
    self.searchBar.clipsToBounds = YES;
    self.searchBar.layer.borderWidth = 1;
    self.searchBar.layer.borderColor = @"#cccccc".toColor.CGColor;
    [view addSubview:self.searchBar];
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(12*PX_RELATIVE375);
        make.top.offset(12);
        make.height.mas_equalTo(32);
        make.right.mas_offset(-5);
    }];
    
    
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.backgroundColor = @"#cccccc".toColor;
    [view addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.offset(0);
        make.height.mas_equalTo(1);
    }];
    
    //切换视图
    [view addSubview:self.segmentedBar];
    [self.segmentedBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(-120);
        make.top.mas_equalTo(self.searchBar.mas_bottom);
        make.bottom.offset(0);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = @"#cccccc".toColor;
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-119);
        make.width.mas_equalTo(1);
        make.top.equalTo(self.searchBar.mas_bottom).offset(8);
        make.bottom.offset(-8);
    }];
    
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:@"商品分类" forState:(UIControlStateNormal)];
    [button setTitleColor:@"#cccccc".toColor forState:(UIControlStateNormal)];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setImage:[UIImage imageNamed:@"select_member"] forState:(UIControlStateNormal)];
    [button setImage:[UIImage imageNamed:@"select_member_on"] forState:(UIControlStateSelected)];
    [view addSubview:button];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 80, 0, 8);
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 32);
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-8);
        make.width.offset(112);
        make.top.equalTo(self.searchBar.mas_bottom);
        make.bottom.offset(0);
    }];
    
    //数据展示视图
    UIScrollView * scrollView = [[UIScrollView alloc] init];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.offset(0);
        make.top.equalTo(view.mas_bottom);
    }];
    _contentView = scrollView;
    [self loadContentSubView];
}

- (void)loadContentSubView{
    ECMaterailManageListViewController *vc1 = [[ECMaterailManageListViewController alloc] init];
    [self.contentView addSubview:vc1.view];
    [self addChildViewController:vc1];
    [vc1.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
        make.width.equalTo(self.contentView);
        make.height.equalTo(self.contentView);
    }];
    
    ECMaterailManageListViewController *vc2 = [[ECMaterailManageListViewController alloc] init];
    vc2.enabled = @"1";
    [self.contentView addSubview:vc2.view];
    [self addChildViewController:vc2];
    [vc2.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.equalTo(vc1.view.mas_right);
        make.width.equalTo(self.contentView);
    }];
    
    ECMaterailManageListViewController *vc3 = [[ECMaterailManageListViewController alloc] init];
    vc3.enabled = @"0";
    [self.contentView addSubview:vc3.view];
    [self addChildViewController:vc3];
    [vc3.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.equalTo(vc2.view.mas_right);
        make.width.equalTo(self.contentView);
        make.right.offset(0);
    }];
}

#pragma mark - getter
- (GLScrollAutoSegmentedBar *)segmentedBar{
    if (_segmentedBar == nil) {
        _segmentedBar = [[GLScrollAutoSegmentedBar alloc] initWithArray:@[@"全部",@"启用",@"禁用"]];
        _segmentedBar.backgroundColor = [UIColor clearColor];
        _segmentedBar.selectColor = @"#00A61E".toColor;
        _segmentedBar.normalColor = @"#999999".toColor;
        _segmentedBar.titleFont = [UIFont systemFontOfSize:13];
        _segmentedBar.lineView.backgroundColor = @"#00A61E".toColor;
    }
    return _segmentedBar;
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [UISearchBar new];
        [_searchBar setImage:[UIImage imageNamed:@"search_member"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        _searchBar.placeholder = @"商品名称";
        [_searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];
        [_searchBar setPositionAdjustment:UIOffsetMake(-6, 0) forSearchBarIcon:UISearchBarIconSearch];
        for (UIView * v in _searchBar.subviews.firstObject.subviews) {
            if ([v isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
                UITextField * tf = (UITextField*)v;
                tf.font = ECFONT(14);
            }
        }
    }
    return _searchBar;
}

@end
